package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Reference
    private IRegionService regionService;

    @Autowired
    private DestinationMapper destinationMapper;

    @Override
    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.getDestByDeep(deep);
    }

    @Override
    public List<Destination> getDestByRegionId(Long rid) {
        //rid == -1  表示查询国内所有省份
        if(rid == -1){
            //id = 1 表示中国id
            return destinationMapper.selectSubDestByParentId(1L);
        }
        //区域
        Region region = regionService.get(rid);
        //关联目的地id
        Long[] refIds = region.getRefIds();
        //关联的目的地
        List<Destination> list = destinationMapper.selectDestByIds(refIds);
        return list;
    }

    @Override
    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = destinationMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        List<Destination> list = new ArrayList<>();
        creatToast(parentId,list);
        Collections.reverse(list);  //列表反序
        return list;
    }

    @Override
    public Destination getCountry(Long id) {
        List<Destination> list = this.getToasts(id);

        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }


    @Override
    public List<Destination> queryMonth(long l) {
        return destinationMapper.selectByMonth(l);
    }

    @Override
    public List<Destination> listDetailAndTheme() {
        return destinationMapper.selectDetailAndTheme();
    }


    @Override
    public List<Destination> queryHotDest() {
        return destinationMapper.queryHotDest();
    }

    @Override
    public List<Destination> queryProvince() {
        return destinationMapper.queryProvince();
    }

    @Override
    public Destination getProvince(Long id) {

        //必须是国内的才设置省份
        List<Destination> list = this.getToasts(id);
        if(list != null && list.size()>0){
            //国家的id不等于1肯定是国外
            if(list.get(0).getId()!=1) {
                return null;
            }
        }

        //吐司里索引为1的是省份
        if(list.size()>1){
            return list.get(1);
        }
        return null;
    }

    private void creatToast(Long parentId, List<Destination> list) {

        //首先查询自己
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if(destination == null){
            return;
        }

        //把自己存进集合里
        list.add(destination);

        //再把自己的父地区查出来再存进集合,有循环调方法直到查不到父地区为止
        if(destination.getParent()!= null){
            creatToast(destination.getParent().getId(),list);
        }
    }
}
